﻿<%@ Page Title="" Language="C#" MasterPageFile="~/EasyUI.Master" AutoEventWireup="true" CodeBehind="contrl.aspx.cs" Inherits="jnlamp.Web.api.contrl" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script src="../Script/jnbase.js"></script>
     <script src="../My97DatePicker/WdatePicker.js" type="text/javascript"></script>
    <link href="../CSS/contrl_btn.css" rel="stylesheet" />
    <style type="text/css">
        .panel {
            float: left;
        }
        .auto-style1 {
            color: #006600;
        }
        .div_sd_style {
            margin-top:15px;
            margin-left:15px; 
            margin-bottom:10px;
            text-align:left
        }
        .auto-style2 {
            color: #FF3300;
        }
    </style>
    <script type="text/javascript">
        var gprs_id = "";
        $(function () {
            gprs_id = getUrlParam("gprs_id");
            //gprs_id = "10001";
            InitGird();
            InitGirdtt2();
            page_load();
            bindDDL();
            //文本框限输入整数              
            $("#txt_ts").keydown(function () {
                //alert(window.event.keyCode);
                var e = $(this).event || window.event;
                var code = parseInt(e.keyCode);
                if (code >= 96 && code <= 105 || code >= 48 && code <= 57 || code == 8) {
                    return true;
                    
                } else {
                    return false;
                }
            });
            $("#cc").autocomplete({
                minLength: 1, // 设置搜索的关键字最小长度// 设置自动完成列表的函数，函数包括两个参数，requset和response      
                source: function (request, response) {
                    $.ajax({
                        type: "POST",          // 通过request.term可以获得文本框内容   
                        url: "hanlder.ashx?action=QueryAutoComplete&gprs_id=" + gprs_id+"&keyword=" + request.term,
                        contentType: "application/json; charset=gb2312",
                        dataType: "json",
                        success: function (data) {
                            // jQuery.map(array, callback) :将一个数组中的元素转换到另一个数组中。
                            //  下面就是把数组["value1", "value2",...]转换为[{value:"value1"}, {value:"value2"},...]                 
                            response($.map(data, function (item) {
                                return { value: item };
                            }));
                        },
                        error: function () {
                            alert("ajax请求失败");
                        }
                    });
                }
            });
            
        });
        function page_load() {
            if (gprs_id != "" && gprs_id != null) {
                $.ajax({
                    type: "post",
                    dataType: "json",
                    url: "hanlder.ashx?action=QueryStatus&gprs_id=" + gprs_id,
                    success: function aa(data) {
                        if (null != data && "[]" != data) {
                            var kdsj = data[0].kdsj;
                            var kdsc = data[0].kdsc;
                            var ts = data[0].ts;
                            var opt = data[0].opt;

                            if (opt == "OPEN01") {
                                opt = "全体开灯";
                            }
                            else if (opt == "OPEN02") {
                                opt = "全体关闭";
                            }
                            else if (opt == "OPEN03") {
                                opt = "奇数开灯";
                            }
                            else if (opt == "OPEN04") {
                                opt = "偶数开灯";
                            }
                            else if (opt == "OPEN05") {
                                opt = "三分之一开灯";
                            }
                            else if (opt == "OPEN06") {
                                opt = "四分之一开灯";
                            }
                            else if (opt == "OPEN09") {
                                opt = "长期关闭";
                            }
                            else if (opt.indexOf("AOPEN") == 0) {
                                opt = "开" + opt.substring(5, 8) + "号灯";
                            }
                            else if (opt.indexOf("ACLOSE") == 0) {
                                opt = "关" + opt.substring(6, 9) + "号灯";
                            }
                            else if (opt == "RESET") {
                                opt = "恢复出厂设置";
                            }
                            

                            if (ts == "0") {
                                ts = "长期运行"
                            }
                            else {
                                ts = "运行天数：" + ts;
                            }

                            kdsj = kdsj.substring(0, 2) + ":" + kdsj.substring(2, 4);
                            kdjc = kdsc.substring(0, 2) + "小时" + kdsc.substring(2, 4) + "分";

                            var content = "当前运行状态：" + opt + ","+ ts + ",开灯时间：" + kdsj + ",开灯时长：" + kdjc + "";

                            $("#lbl_status").html(content);
                        }

                    }
                });

            }

        }
        function bindDDL() {
            if (gprs_id != "" && gprs_id != null) {

                $('#cc').combobox({
                    url: "hanlder.ashx?action=QueryDDl&gprs_id=" + gprs_id,
                    valueField: "scd_id",
                    textField: "scd_id",
                    panelHeight: "auto",
                    editable: false,
                    onSelect: function (record) {
                        $("#cc").val(record.name);
                    },
                    onLoadSuccess: function () { //加载完成后,设置选中第一项
                        var val = $(this).combobox("getData");
                        for (var item in val[0]) {
                            if (item == "scd_id") {
                                $(this).combobox("select", val[0][item]);
                            }
                        }
                    }
                });

            }
        }
        function loadXMLDoc(opt, b) {
            var arr1 = ["01", "02", "03", "04", "05", "06", "07", "08"];
            var arr2 = ["OPEN01", "OPEN02", "OPEN03", "OPEN04", "OPEN05", "OPEN06", "OPEN09", "RESET"];
            for (var i = 0; i < arr1.length; i++) {
                if (b == arr1[i]) {
                    $("#" + arr1[i]).attr("class", "btn btn-sm btn-click rarity");
                    $("#" + arr1[i]).attr("onclick", "");
                } else {
                    $("#" + arr1[i]).attr("class", "btn btn-success btn-sm popover-success rarity");
                    $("#" + arr1[i]).attr("onclick", "loadXMLDoc('" + arr2[i] + "','" + arr1[i] + "')");
                }
            }

            var t = opt;

            $.ajax({
                type: "post",
                dataType: "text",
                url: "hanlder.ashx?action=AddGPRS&opt=" + opt + "&gprs_id=" + gprs_id,
                success: function aa(data) {
                    if (null != data && "[]" != data) {
                        $.messager.alert("提示", "添加成功！", "info");
                        reloadgrid();
                    }

                }
            });

        }
        function ddclick(opt,id) {
            
            var v = $('#cc').combobox('getValue');
            if (v == "") {
                alert("请选择灯号");
            }
            else {
            
                if (v.toString().length == 1) {
                    v = "00" + v;
                }
                else if (v.toString().length == 2) {
                   v = "0" + v;
                }

                $.ajax({
                    type: "post",
                    dataType: "text",
                    url: "hanlder.ashx?action=AddScd&opt=" + (opt+v) + "&gprs_id=" + gprs_id + "&scd_id=" + v,
                    success: function aa(data) {
                        if (null != data && "[]" != data) {
                            if (data == "T") {
                                $.messager.alert("提示", "添加成功！", "info");
                                reloadgrid();
                            }
                          
                        }

                    }
                });
            }

        }
        //初始化表格
        function InitGird() {
            $('#tt').datagrid({
                url: 'hanlder.ashx', //请求数据的页面
                sortName: 'scd_set_opt_id', //排序字段
                idField: 'scd_set_opt_id', //标识字段,主键
                iconCls: '', //标题左边的图标
                width: '100%', //宽度
                //height: '50%', //高度
                nowrap: true, //是否换行，True 就会把数据显示在一行里
                striped: true, //True 奇偶行使用不同背景色
                collapsible: false, //可折叠
                sortOrder: 'desc', //排序类型
                remoteSort: true, //定义是否从服务器给数据排序
                fitColumns: true,
                singleSelect: true,
                scrollbarSize: 0,//滚动条宽度
                autoRowHeight:false,
                method: 'post',
                columns: [[
                    {
                        title: '操作对象', field: 'lamp_id', width: 80, sortable: true, formatter: function (value, row, index) {
                            if (value == "") {
                                return "网关";
                            }
                            else {
                                return value;
                            }
                        }
                    },
                    {
                        title: '指令', field: 'opt', width: 80, formatter: function (value, row, index) {
                            if (value == "OPEN01") {
                                return "全体开灯";
                            }
                            else if (value == "OPEN02") {
                                return "全体关闭";
                            }
                            else if (value == "OPEN03") {
                                return "奇数开灯";
                            }
                            else if (value == "OPEN04") {
                                return "偶数开灯";
                            }
                            else if (value == "OPEN05") {
                                return "三分之一开灯";
                            }
                            else if (value == "OPEN06") {
                                return "四分之一开灯";
                            }
                            else if (value == "OPEN09") {
                                return "长期关闭";
                            }
                            else if (value.indexOf("AOPEN") == 0) {
                                return "开" + value.substring(5, 8) + "号灯";
                            }
                            else if (value.indexOf("ACLOSE") == 0) {
                                return "关" + value.substring(6, 9) + "号灯";
                            }
                            else if (value == "RESET") {
                                return "恢复出厂设置";
                            }
                            else {
                                return value;
                            }
                        }
                    },
                    {
                        title: '读取次数', field: 'readcount', width: 80, formatter: function (value, row, index) {

                            if (row.readcount == 0) {
                                return "3";
                            }
                            else if (row.readcount == 1) {
                                return "2";
                            }
                            else if (row.readcount == 3) {
                                return "0";
                            }
                            else if (row.readcount == 2) {
                                return "1";
                            }
                            else {
                                return row.readcount + "";
                            }



                        }
                    },
                    { title: '读取时间', field: 'readtime', width: 150 },
                    { title: '设置成功次数', field: 'setcount', width: 150 },
                    {
                        title: '操作', field: 'scd_set_opt_id', width: 80, formatter: function (value, row, index) {

                            if (row.readcount == 0) {
                                return "-";
                            }
                            else {
                                return "&nbsp;<a style='color:red' href='javascript:' onclick='delScd(" + row.scd_set_opt_id + ");return false;'>删除</a>";
                            }
                            


                        }
                    }
                ]],
                toolbar: "#tab_toolbar",
                queryParams: { "action": "QueryOpt", "gprs_id": gprs_id },
                pageList: [10, 15, 20, 50, 100],
                pagePosition:'bottom',
                pagination: true, //是否开启分页
                pageNumber: 1, //默认索引页
                pageSize:20, //默认一页数据条数
                rownumbers: true //行号
               
            });


        }
        //初始化表格
        function InitGirdtt2() {
            $('#tt2').datagrid({
                url: 'hanlder.ashx', //请求数据的页面
                sortName: 'scd_set_time_id', //排序字段
                idField: 'scd_set_time_id', //标识字段,主键
                iconCls: '', //标题左边的图标
                width: '100%', //宽度
                height: '100%', //高度
                nowrap: true, //是否换行，True 就会把数据显示在一行里
                striped: true, //True 奇偶行使用不同背景色
                collapsible: false, //可折叠
                sortOrder: 'desc', //排序类型
                remoteSort: true, //定义是否从服务器给数据排序
                fitColumns: true,
                singleSelect: true,
                scrollbarSize: 0,//滚动条宽度
                autoRowHeight: false,
                method: 'post',
                columns: [[
                    {
                        title: '指令（开灯时间/开灯时长/开灯天数）', field: 'kdsj', width: 80, sortable: true, formatter: function (value, row, index) {
                            var kdsj = row.kdsj.substring(0, 2) + ":" + row.kdsj.substring(2, 4);
                            var kdsc = row.kdsc.substring(0, 2) + ":" + row.kdsc.substring(2, 4);
                            
                            return kdsj + "/" + kdsc + "/" + row.ts;
                        }
                    },
                    {
                        title: '读取次数', field: 'readcount', width: 80, formatter: function (value, row, index) {

                            if (row.readcount == 0) {
                                return "3";
                            }
                            else if (row.readcount == 1) {
                                return "2";
                            }
                            else if (row.readcount == 3) {
                                return "0";
                            }
                            else if (row.readcount == 2) {
                                return "1";
                            }
                            else {
                                return row.readcount + "";
                            }



                        }
                    },
                    { title: '读取时间', field: 'readtime', width: 150 },
                    { title: '设置成功次数', field: 'setcount', width: 150 },
                    {
                        title: '操作', field: 'scd_set_time_id', width: 80, formatter: function (value, row, index) {

                            if (row.readcount == 0) {
                                return "-";
                            }
                            else {
                                return "&nbsp;<a style='color:red' href='javascript:' onclick='delScdTime(" + row.scd_set_time_id + ");return false;'>删除</a>";
                            }



                        }
                    }
                ]],
                
                queryParams: { "action": "QueryTime", "gprs_id": gprs_id },
                pageList: [10, 15, 20, 50, 100],
                pagePosition: 'bottom',
                pagination: true, //是否开启分页
                pageNumber: 1, //默认索引页
                pageSize: 20, //默认一页数据条数
                rownumbers: true //行号

            });


        }
        //删除
        function delScd(scd_set_opt_id)
        {
            $.messager.confirm('提示', '你确定要删除这条信息吗？', function (r) {
                if (r) {
                    $.ajax({
                        type: "post",
                        dataType: "text",
                        url: 'hanlder.ashx?action=delScd&scd_set_opt_id=' + scd_set_opt_id,
                        success: function (data) {
                            if (null != data && "[]" != data) {
                                if (data[0] == "T") {
                                    //$.messager.alert("提示", "恭喜您，信息删除成功！", "info");
                                    reloadgrid();
                                }
                                else {
                                    //$.messager.alert("提示", "信息删除失败！", "info");
                                }

                            }
                        }
                    });
                }
               
            });

        }
        //删除
        function delScdTime(scd_set_time_id) {
            $.messager.confirm('提示', '你确定要删除这条信息吗？', function (r) {
                if (r) {
                    $.ajax({
                        type: "post",
                        dataType: "text",
                        url: 'hanlder.ashx?action=delScdTime&scd_set_time_id=' + scd_set_time_id,
                        success: function (data) {
                            if (null != data && "[]" != data) {
                                if (data[0] == "T") {
                                    //$.messager.alert("提示", "恭喜您，信息删除成功！", "info");
                                    reloadgridtt2();
                                }
                                else {
                                    //$.messager.alert("提示", "信息删除失败！", "info");
                                }

                            }
                        }
                    });
                }

            });

        }
        function reloadgrid() {      
            $('#tt').datagrid('reload');
        }
        function reloadgridtt2() {
            $('#tt2').datagrid('reload');
        }      
        function submitTime()
        {
            var ts = $("#txt_ts").val();
            if (parseInt(ts) >=0 && parseInt(ts) <= 99) {
                var kdsj = $("#txt_kdsj").val();
                var xs = $("#txt_xs").val();
                var fz = $("#txt_fz").val();

                if ((xs == "" && fz == "") || (xs == "00" && fz == "00")) {
                    alert("开灯时长不能为0！");
                    return;
                }
                if (kdsj == "") {
                    kdsj = "00:00";
                }
                if (xs == "") {
                    xs = "00";
                }
                if (fz == "") {
                    fz = "00";
                }

                kdsj = kdsj.substring(0, 2) + kdsj.substring(3, 5);
                var kdsc = xs + fz;

                $.ajax({
                    type: "post",
                    dataType: "text",
                    url: "hanlder.ashx?action=AddScdTime&kdsj=" + kdsj + "&gprs_id=" + gprs_id + "&kdsc=" + kdsc+"&ts="+ts,
                    success: function aa(data) {
                        if (null != data && "[]" != data) {
                            if (data == "T") {
                                $.messager.alert("提示", "添加成功！", "info");
                                reloadgridtt2();
                            }
                        }

                    }
                });


            }
            else {
                alert("天数请输入99以内的正整数!");
            }

        }

    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div style="width: 100%; height: 100%; overflow-y: auto; overflow-x: hidden; text-align: center;">
        <div id="dlg" class="easyui-panel" title="当前运行状态" style="width: 100%; height: 70px; overflow: auto;">
            <span id="lbl_status" name="lbl_status" style="font-size: 14pt; margin-top: 20px; margin-left: 30px"></span>
        </div>

        <div id="Div1" class="easyui-panel" title="群体操作" style="width: 48%; height: 90%; overflow: auto; ">
            <div style="width: 100%;">
                <div>
                    <span title="" id="01" name="01" class="btn btn-success btn-sm popover-success rarity" onclick="loadXMLDoc('OPEN01','01');">全体开灯</span>
                    <span title="" id="02" name="02" class="btn btn-success btn-sm popover-success rarity" onclick="loadXMLDoc('OPEN02','02');">全体关灯</span>
                    <span title="" id="03" name="03" class="btn btn-success btn-sm popover-success rarity" onclick="loadXMLDoc('OPEN03','03');">奇数开灯</span>
                    <span title="" id="04" name="04" class="btn btn-success btn-sm popover-success rarity" onclick="loadXMLDoc('OPEN04','04');">偶数开灯</span>
                    <span title="" id="05" name="05" class="btn btn-success btn-sm popover-success rarity" onclick="loadXMLDoc('OPEN05','05');">1 / 3 开灯</span>
                    <span title="" id="06" name="06" class="btn btn-success btn-sm popover-success rarity" onclick="loadXMLDoc('OPEN06','06');">1 / 4 开灯</span>
                    <span title="" id="07" name="07" class="btn btn-success btn-sm popover-success rarity" onclick="loadXMLDoc('OPEN09','07');">长期关闭</span>
                    <span title="" id="08" name="08" class="btn btn-success btn-sm popover-success rarity" onclick="loadXMLDoc('RESET','08');">恢复出厂</span>
                </div>
                <div style="text-align: center; font-size: 12px">
                    单灯操作：<input id="cc" class="easyui-combobox"  name="dept">
                    <%--<input id="cc" type="text"/>--%>
                </div>
                <div style="text-align: center; font-size: 12px">
                    <span title="" id="09" name="09" class="btn btn-success btn-sm popover-success rarity" onclick="ddclick('AOPEN','09')">单灯开启</span>
                    <span title="" id="10" name="10" class="btn btn-success btn-sm popover-success rarity" onclick="ddclick('ACLOSE','10')">单灯关闭</span>
                </div>
            </div>
            <div style="height: 100%;">
                <table id="tt"></table>
            </div>
        </div>
        <div id="Div3" class="easyui-panel" style="width: 1%; height: 90%; overflow: auto;">
        </div>
        <div id="Div2" class="easyui-panel" title="时段设置" style="width: 49%; height: 90%; overflow: auto;">
            <div style="width: 100%;">
                 <div class="div_sd_style">
                     <span class="auto-style1"><strong>开灯时间：</strong></span>
                     <input type="text" id="txt_kdsj" name="txt_kdsj" onfocus="WdatePicker({dateFmt:'HH:mm',readOnly:true})"/>
                 </div>
                 <div class="div_sd_style">
                     <span class="auto-style1"><strong>开灯时长：</strong></span>
                     <input type="text" id="txt_xs" name="txt_xs" onfocus="WdatePicker({dateFmt:'HH',readOnly:true})" style="width: 28px"/>
                     <strong>&nbsp;小时&nbsp;
                     <input type="text" id="txt_fz" name="txt_fz" onfocus="WdatePicker({dateFmt:'mm',readOnly:true})" style="width: 28px"/>&nbsp;分</strong></div>
                 <div class="div_sd_style">
                     <span class="auto-style1"><strong>开灯天数：</strong></span>
                     <input type="text" style="ime-mode:disabled" id ="txt_ts" name="txt_ts"   />
                 <span class="auto-style2">*只能输入数字且不能大于99,0为永久开启</span>
                 </div>
                <div class="div_sd_style">
                     <span  class="btn btn-success btn-sm popover-success rarity" onclick="submitTime()">&nbsp;&nbsp;提&nbsp;&nbsp;交&nbsp;&nbsp;</span>
                </div>
            </div>
            <div style="height:100%; ">
                <table id="tt2"></table>
            </div>

        </div>
    </div>
</asp:Content>
